package projectEuler;

public class Problem_14 {
        public static void main(String[] args) {
                long[] numbers = new long[1000001];
                numbers[1] = 1;
                numbers[0] = 0;
                int highestIteration = 0;
                int highestIterationNumber = 0;
                for(int count = 2; count < 1000001; count ++){
                        long n = count;
                        long numberOfIterations = 0;
                        while(n != 1){  
                                if(n%2==0){//if n is even
                                        n = n/2;
                                }else{
                                        n = 3*n + 1;
                                }
                                if(n < count){
                                        try{
                                                numberOfIterations = numberOfIterations + numbers[(int)n];
                                        }catch(Exception e){
                                                System.out.println("ERROR @ "+count+" "+n);

                                        }
                                        n = 1;
                                }else{
                                        numberOfIterations++;
                                }
                        }
                        numbers[count] = numberOfIterations;
                        if(numberOfIterations > highestIteration){
                                highestIteration = (int) numberOfIterations;
                                highestIterationNumber = count;
                        }
                        numberOfIterations = 0;
                }
                System.out.println(highestIterationNumber);
        }
}